/*
 * @Author: fengxx
 * @Date: 2025-09-05 09:19:27
 * @LastEditors: fengxx
 * @LastEditTime: 2025-10-17 00:58:15
 */
import { ref, computed, type Ref } from 'vue'
import axios from 'axios'
import { defineStore } from 'pinia'
import { useProductStore } from './product'

interface Classifys {
  met: string[]
  names: string[]
  price: number[]
}

export const useCounterStore = defineStore('counter', () => {
  const productStore = useProductStore()
  // state
  const count: Ref<number> = ref(1)
  const num = ref(9)
  const classfys: Ref<Classifys> = ref({
    met: [],
    names: [],
    price: [],
  })
  // getter
  const doubleCount = computed(() => count.value * 2)
  const newCount = computed(() => doubleCount.value + 1)
  // getter的值由其他的store获取过来的值计算
  const otherCount = computed(() => {
    return (newCount: number) =>
      count.value == newCount ? productStore.doubleCount : productStore.count
  })
  // action
  function increment() {
    // count.value++
    axios
      .get('/api/data', {
        // headers: {
        //   'Content-Type': 'application/json;charset=UTF-8',
        // },
      })
      .then((response) => {
        console.log(response.data.data)
        // this.userList = response.data.data // 假设你的数据结构是这样的 {'status': 200, 'data': []}
      })
      .catch((error) => {
        console.error('There was an error!', error)
      })
  }

  const getCount = async () => {
    productStore.increment()
    fetch('https://mock.mengxuegu.com/mock/6888141205d8245baf88ff7e/example/test')
      .then((response) => response.json())
      .then((data) => {
        // console.log(data)
        classfys.value = data.data
      })
      .catch((error) => console.error('Error:', error))
    // const
    count.value = 1
  }

  function $reset() {
    count.value = 0
  }

  return { count, num, classfys, doubleCount, newCount, otherCount, increment, getCount, $reset }
})
